#include <rtthread.h>
#include <rtdevice.h>
#include "drv_common.h"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

#define RELAY1_PIN GET_PIN(A, 4)
#define RELAY2_PIN GET_PIN(E, 6)
#define RELAY3_PIN GET_PIN(B, 7)
#define RELAY4_PIN GET_PIN(A, 6)
#define RELAY5_PIN GET_PIN(A, 8)
#define RELAY6_PIN GET_PIN(F, 10)
#define RELAY7_PIN GET_PIN(B, 10)
#define RELAY8_PIN GET_PIN(E, 5)

static rt_uint8_t s_cmd = 0;



void BoardRelay_Init(void)
{
    rt_pin_mode(RELAY1_PIN, PIN_MODE_OUTPUT);
    rt_pin_mode(RELAY2_PIN, PIN_MODE_OUTPUT);
    rt_pin_mode(RELAY3_PIN, PIN_MODE_OUTPUT);
    rt_pin_mode(RELAY4_PIN, PIN_MODE_OUTPUT);
    rt_pin_mode(RELAY5_PIN, PIN_MODE_OUTPUT);
    rt_pin_mode(RELAY6_PIN, PIN_MODE_OUTPUT);
    rt_pin_mode(RELAY7_PIN, PIN_MODE_OUTPUT);
    rt_pin_mode(RELAY8_PIN, PIN_MODE_OUTPUT);
}

INIT_ENV_EXPORT(BoardRelay_Init);

rt_uint8_t getRelayStatus()
{
    return s_cmd;
}

void setRelayStatus(rt_uint8_t cmd)
{
    s_cmd = cmd;

    if((cmd&0x01) == 0x01)
    {
        rt_pin_write(RELAY1_PIN, PIN_HIGH);
    }
    else
    {
        rt_pin_write(RELAY1_PIN, PIN_LOW);
    }

    if((cmd&0x02) == 0x02)
    {
        rt_pin_write(RELAY2_PIN, PIN_HIGH);
    }
    else
    {
        rt_pin_write(RELAY2_PIN, PIN_LOW);
    }

    if((cmd&0x04) == 0x04)
    {
        rt_pin_write(RELAY3_PIN, PIN_HIGH);
    }
    else
    {
        rt_pin_write(RELAY3_PIN, PIN_LOW);
    }

    if((cmd&0x08) == 0x08)
    {
        rt_pin_write(RELAY4_PIN, PIN_HIGH);
    }
    else
    {
        rt_pin_write(RELAY4_PIN, PIN_LOW);
    }

    if((cmd&0x10) == 0x10)
    {
        rt_pin_write(RELAY5_PIN, PIN_HIGH);
    }
    else
    {
        rt_pin_write(RELAY5_PIN, PIN_LOW);
    }

    if((cmd&0x20) == 0x20)
    {
        rt_pin_write(RELAY6_PIN, PIN_HIGH);
    }
    else
    {
        rt_pin_write(RELAY6_PIN, PIN_LOW);
    }

    if((cmd&0x40) == 0x40)
    {
        rt_pin_write(RELAY7_PIN, PIN_HIGH);
    }
    else
    {
        rt_pin_write(RELAY7_PIN, PIN_LOW);
    }

    if((cmd&0x80) == 0x80)
    {
        rt_pin_write(RELAY8_PIN, PIN_HIGH);
    }
    else
    {
        rt_pin_write(RELAY8_PIN, PIN_LOW);
    }
}

void BoardRelay_Open(rt_uint8_t cmd)
{
    if((cmd&0x01) == 0x01)
    {
        rt_pin_write(RELAY1_PIN, PIN_HIGH);
    }
    if((cmd&0x02) == 0x02)
    {
        rt_pin_write(RELAY2_PIN, PIN_HIGH);
    }
    if((cmd&0x04) == 0x04)
    {
        rt_pin_write(RELAY3_PIN, PIN_HIGH);
    }
    if((cmd&0x08) == 0x08)
    {
        rt_pin_write(RELAY4_PIN, PIN_HIGH);
    }
    if((cmd&0x10) == 0x10)
    {
        rt_pin_write(RELAY5_PIN, PIN_HIGH);
    }
    if((cmd&0x20) == 0x20)
    {
        rt_pin_write(RELAY6_PIN, PIN_HIGH);
    }
    if((cmd&0x40) == 0x40)
    {
        rt_pin_write(RELAY7_PIN, PIN_HIGH);
    }
    if((cmd&0x80) == 0x80)
    {
        rt_pin_write(RELAY8_PIN, PIN_HIGH);
    }
}

void BoardRelay_Close(rt_uint8_t cmd)
{
    if((cmd&0x01) == 0x01)
    {
        rt_pin_write(RELAY1_PIN, PIN_LOW);
    }
    if((cmd&0x02) == 0x02)
    {
        rt_pin_write(RELAY2_PIN, PIN_LOW);
    }
    if((cmd&0x04) == 0x04)
    {
        rt_pin_write(RELAY3_PIN, PIN_LOW);
    }
    if((cmd&0x08) == 0x08)
    {
        rt_pin_write(RELAY4_PIN, PIN_LOW);
    }
    if((cmd&0x10) == 0x10)
    {
        rt_pin_write(RELAY5_PIN, PIN_LOW);
    }
    if((cmd&0x20) == 0x20)
    {
        rt_pin_write(RELAY6_PIN, PIN_LOW);
    }
    if((cmd&0x40) == 0x40)
    {
        rt_pin_write(RELAY7_PIN, PIN_LOW);
    }
    if((cmd&0x80) == 0x80)
    {
        rt_pin_write(RELAY8_PIN, PIN_LOW);
    }
}


static int relay_test(int argc, char *argv[])
{
    rt_err_t ret = RT_EOK;

    if (argc == 2)
    {
        if(argv[0] == '0')
        {
            BoardRelay_Close((uint8_t)argv[1]);
        }
        else if(argv[0] == '1')
        {
            BoardRelay_Open((uint8_t)argv[1]);
        }
    }

    return ret;
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(relay_test,RelayTest);
